﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[proc_LoginJL]
	(
	@loginname NVARCHAR(50),
	@loginuserid VARCHAR(50),
	@userCompanyId INT,
	@userComapnyName NVARCHAR (500),
	@IP VARCHAR(50),
	@userids NVARCHAR (500)
	)
AS
BEGIN
	
	DECLARE @str_dltime DATETIME,@str_dlusers VARCHAR(500),@str_flag INT
	SET @str_flag=0    
	IF CHARINDEX('127.0',@IP,0)=0
	BEGIN
	SELECT TOP 1 @str_dltime=logindate,@str_dlusers=memo
	  FROM [LoginJL] WHERE loginuserid= @loginuserid AND ip=@IP ORDER BY id DESC 
	  	  
	  IF @str_dlusers=@userids -- 登录过的账号和已存入的账号相同
	  BEGIN
	  	IF CONVERT(VARCHAR(10),@str_dltime,120)=CONVERT(VARCHAR(10),GETDATE(),120) and DATEDIFF(hour,@str_dltime,GETDATE())>4 --登录间隔4小时
	  	BEGIN
	  		SET @str_flag=1
	  	END      
	  	ELSE IF CONVERT(VARCHAR(10),@str_dltime,120)!=CONVERT(VARCHAR(10),GETDATE(),120) --不是同一天
	  		BEGIN
	  			SET @str_flag=1
	  		END                                   
	  END
	  ELSE
	  	BEGIN
	  		SET @str_flag=1
	  	END
	 END 	
IF @str_flag=1
BEGIN    
	    
	INSERT INTO [LoginJL]
           ([loginname]
           ,[loginuserid]
           ,[userCompanyId]
           ,[userComapnyName]
           ,[logindate]
           ,[IP]
           ,memo)
     VALUES
           (@loginname
           ,@loginuserid
           ,@userCompanyId
           ,@userComapnyName
           ,GETDATE()
           ,@IP
            ,@userids)
END
END
RETURN
